//
//  MyView.m
//  cooba-iOS
//
//  Created by 郭四虎 on 16/9/12.
//  Copyright © 2016年 YouKu. All rights reserved.
//

#import "MyView.h"

@implementation MyView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

*/
- (instancetype)initWithFrame:(CGRect)frame startPoint:(CGPoint)startP msg:(NSString *)msg
{
    if (self = [super initWithFrame:frame]) {
        
        _msg = msg;
        _startPoint = startP;
        self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.2];
        
        _msgLabel = [[UILabel alloc]init];
        _msgLabel.text = msg;
        _msgLabel.font = [UIFont systemFontOfSize:14];
        _msgLabel.numberOfLines = 0;
        
        CGFloat wid = 195;
        
        CGSize size =  [msg boundingRectWithSize:CGSizeMake(wid, MAXFLOAT) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_msgLabel.font} context:nil].size;
        _msgLabel.frame = (CGRect){{5,5},{size.width,size.height}};
        
        UIView *vi = [[UIView alloc]initWithFrame:CGRectMake(startP.x-wid,startP.y-10, _msgLabel.width+10, _msgLabel.height+10)];
        vi.backgroundColor = [UIColor whiteColor];
        [vi addSubview:_msgLabel];
        
        vi.layer.cornerRadius = 10;
        
        [self addSubview:vi];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    
    CGContextRef ctf = UIGraphicsGetCurrentContext();
    
    CGContextMoveToPoint(ctf, _startPoint.x-30, _startPoint.y-9);
    CGContextAddLineToPoint(ctf, _startPoint.x-10, _startPoint.y-9);
    CGContextAddLineToPoint(ctf, _startPoint.x-7, _startPoint.y-20);
    
    [[UIColor whiteColor]setFill];
    CGContextFillPath(ctf);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self removeFromSuperview];
}

@end
